import { gdclass, signal } from "../../decorators";

// class注解必须
@gdclass
export default class MoveSignal extends godot.Node2D{
    // 注册信号
    @signal
    static readonly onUpdatePosition:string = "_on_update_pos";

    x = 200
    y = 200
    v = 3

    isInput = false;
    isKeyMove = false;
    isMouseMove = false;

    constructor(
        x=200,y=200,
        isInput = false,
        isKey = false,
        isMouse = false
    ){
        super();

        this.x = x;
        this.y =y
        
        this.isInput = isInput
        this.isKeyMove = isKey
        this.isMouseMove = isMouse
    }

     // 物理帧 严格的60hz(1s执行60次) 事件处理 位置相关
     _physics_process(delta: number): void {
        if(this.get_parent() == null){
            return;
        }

        if (this.isKeyMove) {
            // this.handleKey()
            this.handlekAction()
        }

        if (this.isMouseMove) {
            if (godot.Input.is_mouse_button_pressed(godot.BUTTON_LEFT)) {
                let mousePos = this.get_viewport().get_mouse_position()
                this.x = mousePos.x
                this.y = mousePos.y
                this.updatePosition();
            }
        }

    }

    handleKey() {
        let face = -1
        // InputAction
        if (godot.Input.is_key_pressed(godot.KEY_W)) {
            face = 3
            this.y = this.y - this.v
        } else if (godot.Input.is_key_pressed(godot.KEY_A)) {

            face = 1
            this.x = this.x - this.v
        } else if (godot.Input.is_key_pressed(godot.KEY_S)) {

            face = 0
            this.y = this.y + this.v
        } else if (godot.Input.is_key_pressed(godot.KEY_D)) {
            face = 2
            this.x = this.x + this.v
        }
        this.updatePosition();

    }

    handlekAction() {
        let face = -1
        // InputAction
        if (godot.Input.is_action_pressed("ui_up")) {
            face = 3
            this.y = this.y - this.v
        } else if (godot.Input.is_action_pressed("ui_left")) {
            face = 1
            this.x = this.x - this.v
        } else if (godot.Input.is_action_pressed("ui_down")) {
            face = 0
            this.y = this.y + this.v
        } else if (godot.Input.is_action_pressed("ui_right")) {
            face = 2
            this.x = this.x + this.v
        }
        this.updatePosition();
    }

    updatePosition(){
        this.emit_signal(MoveSignal.onUpdatePosition,this.x,this.y)
    }
}